<?php

namespace App\Services;

use App\Constant;
use App\Models\AgentConfig;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Arr;

class XunChengService
{
    /** @var AgentService */
    private $agentService;
    private $apiHost = 'https://v.apistore.cn/api';
    private $ocrAppKey;
    private $realNameAppKey;
    private $smsAppKey;
    private $verifyCodeTemplateId;

    /**
     * XunChengService constructor.
     */
    public function __construct(AgentService $agentService)
    {
        $this->agentService = $agentService;
    }

    /** 设置代理ID */
    public function setAgentId($agentId)
    {
        $agentSettings = $this->agentService->getAgentSettings($agentId, [
            AgentConfig::XUNCHENG_OCR_APPKEY,
            AgentConfig::XUNCHENG_REALNAME_APPKEY,
            AgentConfig::XUNCHENG_SMS_APPKEY,
            AgentConfig::XUNCHENG_VERIFY_CODE_TEMPLATE,
        ]);
        $this->ocrAppKey = Arr::get($agentSettings, AgentConfig::XUNCHENG_OCR_APPKEY);
        $this->realNameAppKey = Arr::get($agentSettings, AgentConfig::XUNCHENG_REALNAME_APPKEY);
        $this->smsAppKey = Arr::get($agentSettings, AgentConfig::XUNCHENG_SMS_APPKEY);
        $this->verifyCodeTemplateId = Arr::get($agentSettings, AgentConfig::XUNCHENG_VERIFY_CODE_TEMPLATE);
    }

    /** 实名认证 */
    public function realNameAuth($cardNo, $realName)
    {
        $url = $this->apiHost . '/a1';
        $query = [
            'key' => $this->realNameAppKey,
            'cardNo' => $cardNo,
            'realName' => $realName,
            'information' => 1,
            'account' => 'auto',
        ];
        \Log::channel(Constant::LOG_CHANNEL_XUNCHENG)->info('REAL_NAME_REQUEST: GET ' . $url, $query);
        $resp = \Http::get($url, $query);
        \Log::channel(Constant::LOG_CHANNEL_XUNCHENG)->info('REAL_NAME_RESP:' . $resp->body());

        return $this->parseResp($resp);
    }

    /** 身份证正面OCR识别 */
    public function idCardFrontOcr($base64String)
    {
        $url = $this->apiHost . '/c9';
        $query = [
            'key' => $this->ocrAppKey,
            'base64String' => $base64String,
            'side' => 1,
            'account' => 'auto',
        ];
        \Log::channel(Constant::LOG_CHANNEL_XUNCHENG)->info('ID_CARD_FRONT_OCR_REQUEST: APP_KEY=' . $this->ocrAppKey);
        $resp = \Http::post($url, $query);
        \Log::channel(Constant::LOG_CHANNEL_XUNCHENG)->info('ID_CARD_FRONT_OCR_RESP:' . $resp->body());

        return $this->parseResp($resp);
    }

    /** 身份证背面OCR识别 */
    public function idCardBackOcr($base64String)
    {
        $url = $this->apiHost . '/c9';
        $query = [
            'key' => $this->ocrAppKey,
            'base64String' => $base64String,
            'side' => 2,
            'account' => 'auto',
        ];
        \Log::channel(Constant::LOG_CHANNEL_XUNCHENG)->info('ID_CARD_BACK_OCR_REQUEST: APP_KEY=' . $this->ocrAppKey);
        $resp = \Http::post($url, $query);
        \Log::channel(Constant::LOG_CHANNEL_XUNCHENG)->info('ID_CARD_BACK_OCR_RESP:' . $resp->body());

        return $this->parseResp($resp);
    }

    /** 发送短信验证码 */
    public function sendVerifyCode($mobile, $code)
    {
        $url = $this->apiHost . '/v14/xsend';
        if (!$this->smsAppKey) {
            throw new \Exception('寻程短信应用密钥未配置');
        }
        $query = [
            'key' => $this->smsAppKey,
            'mobile' => $mobile,
            'tpl_id' => $this->verifyCodeTemplateId,
            'tpl_val' => json_encode(['code' => $code]),
        ];
        \Log::channel(Constant::LOG_CHANNEL_XUNCHENG)->info('SMS_REQUEST: APP_KEY=' . $this->smsAppKey);
        $resp = \Http::post($url, $query);
        \Log::channel(Constant::LOG_CHANNEL_XUNCHENG)->info('SMS_RESP:' . $resp->body());

        return $this->parseResp($resp);
    }

    private function parseResp(Response $resp)
    {
        $data = $resp->json();
        if (0 === Arr::get($data, 'error_code')) {
            return Arr::get($data, 'result');
        }

        throw new \Exception(Arr::get($data, 'reason'));
    }
}
